package com.qiezikaka.kaka.ui.bind;

import android.graphics.Color;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import com.qiezikaka.kaka.R;
import com.qiezikaka.kaka.base.BaseActivity;
import com.qiezikaka.kaka.base.IPresenter;
import com.qiezikaka.kaka.bean.AllBean;
import com.qiezikaka.kaka.bean.ShopInfo;
import com.qiezikaka.kaka.databinding.ActivityBindStoreBinding;

/**
 * 绑定商家
 */
public class BindStoreActivity extends BaseActivity<BindStorePresenter> implements BindStoreContract.View {
    ActivityBindStoreBinding mBinding;
    private BindStorePresenter presenter;
    private String shopId;
    private String androidId;
    private boolean isBindStore = true;

    @Override
    public View getLayout() {
        mBinding = ActivityBindStoreBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    protected BindStorePresenter getPresenter() {
        if (presenter == null) {
            presenter = new BindStorePresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        androidId = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        presenter.isBind(androidId);

        mBinding.tvBtnSeach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String machineSn = mBinding.etMachineSn.getText().toString().trim();
                if(TextUtils.isEmpty(machineSn)){
                    showToast("商家编号不能为空");
                    return;
                }
                presenter.getShopInfo(machineSn);
            }
        });
        mBinding.tvBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(shopId)){
                    presenter.bindShop(androidId,shopId);
                }else{
                    showToast("请先查询商家信息");
                }
            }
        });
        mBinding.tvBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    /**
     * 绑定商家
     * @param info
     */
    @Override
    public void onBindShop(AllBean info) {
        switch (info.getStatus()){
            case 200:
                showToast("绑定成功");
                finish();
                break;
            case 422:
                showToast(info.getMessage());
                break;
        }
    }

    /**
     * 是否绑定设备
     * @param info
     */
    @Override
    public void onIsBind(AllBean info) {
        switch (info.getStatus()){
            case 200:
                isBindStore = true;
                break;
            case 401: //未绑定
                isBindStore = false;
                break;
        }
    }

    /**
     * 获取商家信息
     * @param info
     */
    @Override
    public void onGetShop(ShopInfo info) {
        switch (info.getStatus()){
            case 200:
                ShopInfo.DataDTO infoData = info.getData();
                if(infoData != null){
                    shopId = infoData.getId();
                    mBinding.llStopView.setVisibility(View.VISIBLE);
                    mBinding.tvBtnSubmit.setVisibility(View.VISIBLE);
                    mBinding.tvShopName.setText(infoData.getName());
                    mBinding.tvShopName.setTextColor(getResources().getColor(R.color.gray01));
                }
                break;
            case 422:
                mBinding.llStopView.setVisibility(View.VISIBLE);
                mBinding.tvBtnSubmit.setVisibility(View.GONE);
                mBinding.tvShopName.setText("商家不存在");
                mBinding.tvShopName.setTextColor(Color.RED);
                showToast(info.getMessage());
                break;
        }
    }
}
